package com.lgh.demo.viewModel;

import android.content.Intent;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.lgh.demo.R;
import com.lgh.demo.activity.HistoryActivity;
import com.lgh.demo.activity.SubmitActivity;
import com.lgh.demo.bean.BeanHistoryOrder;
import com.lgh.demo.databinding.ActivityHistoryBinding;
import com.lgh.demo.util.ActivityUtils;
import com.lgh.demo.util.ImageToos;
import com.lgh.demo.util.Tools;
import com.lgh.demo.util.WMConstantUtil;
import com.lgh.demo.view.BorderView;

import java.util.ArrayList;
import java.util.List;

public class VMHistory {

    private HistoryActivity historyActivity;
    private ActivityHistoryBinding historyBinding;
    private List<BeanHistoryOrder> historyOrders;

    public VMHistory(HistoryActivity historyActivity, ActivityHistoryBinding historyBinding) {
        this.historyActivity = historyActivity;
        this.historyBinding = historyBinding;

        setItemView();
        initBorderView();
    }

    private void initBorderView(){
        BorderView borderView = new BorderView(historyActivity);
        borderView.setBackgroundResource(R.drawable.focus_default);
        borderView.attachTo(historyBinding.flContent);
    }

    private void setItemView() {
        String historyStr = Tools.getStringPreference(historyActivity, WMConstantUtil.KEY_HISTORY_ORDER);
        historyOrders = JSONObject.parseArray(historyStr, BeanHistoryOrder.class);
        if (historyOrders==null){
            historyOrders = new ArrayList<>();
        }
        for (int i = 0; i < historyOrders.size(); i++) {
            BeanHistoryOrder beanHistoryOrder = historyOrders.get(i);
            setHistoryData(beanHistoryOrder, i);
        }

        for (int i=historyOrders.size(); i<6; i++){
            historyBinding.flContent.getChildAt(i).setVisibility(View.GONE);
        }

    }

    private void setHistoryData(BeanHistoryOrder beanHistoryOrder, int pos) {
        int foodCount = beanHistoryOrder.getPreviewOrder().getPreviewFoodItemList().size();
        for (int i = 0; i < foodCount; i++) {
            BeanHistoryOrder.PreviewOrderBean.PreviewFoodItemListBean foodItem =
                    beanHistoryOrder.getPreviewOrder().getPreviewFoodItemList().get(i);
            switch (pos) {
                case 0: {
                    ImageToos.loadImage(historyBinding.includeSeller1.ivPoi, foodItem.getPicture());
                    historyBinding.includeSeller1.tvName.setText(beanHistoryOrder.getPreviewOrder().getOrderSummary().getPoiName());
                    historyBinding.includeSeller1.tvInfoAll.setText("共"+ foodCount
                            +"件商品，实付￥"+beanHistoryOrder.getPreviewOrder().getOrderSummary().getTotal());
                    switch (i) {
                        case 0:
                            historyBinding.includeSeller1.tvInfo1.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 1:
                            historyBinding.includeSeller1.tvInfo2.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 2:
                            historyBinding.includeSeller1.tvInfo3.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 3:
                            historyBinding.includeSeller1.tvInfo4.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 4:
                            historyBinding.includeSeller1.tvInfo5.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 5:
                            historyBinding.includeSeller1.tvInfo6.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                    }
                }
                break;
                case 1: {
                    ImageToos.loadImage(historyBinding.includeSeller2.ivPoi, foodItem.getPicture());
                    historyBinding.includeSeller2.tvName.setText(beanHistoryOrder.getPreviewOrder().getOrderSummary().getPoiName());
                    historyBinding.includeSeller2.tvInfoAll.setText("共"+ foodCount
                            +"件商品，实付￥"+beanHistoryOrder.getPreviewOrder().getOrderSummary().getTotal());
                    switch (i) {
                        case 0:
                            historyBinding.includeSeller2.tvInfo1.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 1:
                            historyBinding.includeSeller2.tvInfo2.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 2:
                            historyBinding.includeSeller2.tvInfo3.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 3:
                            historyBinding.includeSeller2.tvInfo4.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 4:
                            historyBinding.includeSeller2.tvInfo5.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 5:
                            historyBinding.includeSeller2.tvInfo6.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                    }
                }
                break;
                case 2: {
                    ImageToos.loadImage(historyBinding.includeSeller3.ivPoi, foodItem.getPicture());
                    historyBinding.includeSeller3.tvName.setText(beanHistoryOrder.getPreviewOrder().getOrderSummary().getPoiName());
                    historyBinding.includeSeller3.tvInfoAll.setText("共"+ foodCount
                            +"件商品，实付￥"+beanHistoryOrder.getPreviewOrder().getOrderSummary().getTotal());
                    switch (i) {
                        case 0:
                            historyBinding.includeSeller3.tvInfo1.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 1:
                            historyBinding.includeSeller3.tvInfo2.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 2:
                            historyBinding.includeSeller3.tvInfo3.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 3:
                            historyBinding.includeSeller3.tvInfo4.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 4:
                            historyBinding.includeSeller3.tvInfo5.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 5:
                            historyBinding.includeSeller3.tvInfo6.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                    }
                }
                break;
                case 3: {
                    ImageToos.loadImage(historyBinding.includeSeller4.ivPoi, foodItem.getPicture());
                    historyBinding.includeSeller4.tvName.setText(beanHistoryOrder.getPreviewOrder().getOrderSummary().getPoiName());
                    historyBinding.includeSeller4.tvInfoAll.setText("共"+ foodCount
                            +"件商品，实付￥"+beanHistoryOrder.getPreviewOrder().getOrderSummary().getTotal());
                    switch (i) {
                        case 0:
                            historyBinding.includeSeller4.tvInfo1.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 1:
                            historyBinding.includeSeller4.tvInfo2.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 2:
                            historyBinding.includeSeller4.tvInfo3.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 3:
                            historyBinding.includeSeller4.tvInfo4.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 4:
                            historyBinding.includeSeller4.tvInfo5.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 5:
                            historyBinding.includeSeller4.tvInfo6.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                    }
                }
                break;
                case 4: {
                    ImageToos.loadImage(historyBinding.includeSeller5.ivPoi, foodItem.getPicture());
                    historyBinding.includeSeller5.tvName.setText(beanHistoryOrder.getPreviewOrder().getOrderSummary().getPoiName());
                    historyBinding.includeSeller5.tvInfoAll.setText("共"+ foodCount
                            +"件商品，实付￥"+beanHistoryOrder.getPreviewOrder().getOrderSummary().getTotal());
                    switch (i) {
                        case 0:
                            historyBinding.includeSeller5.tvInfo1.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 1:
                            historyBinding.includeSeller5.tvInfo2.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 2:
                            historyBinding.includeSeller5.tvInfo3.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 3:
                            historyBinding.includeSeller5.tvInfo4.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 4:
                            historyBinding.includeSeller5.tvInfo5.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 5:
                            historyBinding.includeSeller5.tvInfo6.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                    }
                }
                break;
                case 5: {
                    ImageToos.loadImage(historyBinding.includeSeller6.ivPoi, foodItem.getPicture());
                    historyBinding.includeSeller6.tvName.setText(beanHistoryOrder.getPreviewOrder().getOrderSummary().getPoiName());
                    historyBinding.includeSeller6.tvInfoAll.setText("共"+ foodCount
                            +"件商品，实付￥"+beanHistoryOrder.getPreviewOrder().getOrderSummary().getTotal());
                    switch (i) {
                        case 0:
                            historyBinding.includeSeller6.tvInfo1.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 1:
                            historyBinding.includeSeller6.tvInfo2.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 2:
                            historyBinding.includeSeller6.tvInfo3.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 3:
                            historyBinding.includeSeller6.tvInfo4.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 4:
                            historyBinding.includeSeller6.tvInfo5.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                        case 5:
                            historyBinding.includeSeller6.tvInfo6.setText(foodItem.getFoodName() + " X" + foodItem.getCount());
                            break;
                    }
                }
                break;
            }


        }
    }

    public void gotoDetail(View view) {
        Intent intent = new Intent(historyActivity, SubmitActivity.class);
        intent.putExtra("history", true);
        ActivityUtils.gotoActivityWithIntent(historyActivity, intent);
    }

}
